<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import AuthService from '../services/AuthService.js'

const router = useRouter()
const username = ref('')
const email = ref('')
const password = ref('')
const confirmPassword = ref('')
const errorMessage = ref('')
const successMessage = ref('')
const isLoading = ref(false)

// 表单验证函数
const validateForm = () => {
  if (!username.value.trim()) {
    errorMessage.value = '请输入用户名'
    return false
  }
  if (!email.value.trim()) {
    errorMessage.value = '请输入邮箱'
    return false
  }
  // 使用认证服务验证邮箱格式
  if (!AuthService.validateEmail(email.value)) {
    errorMessage.value = '请输入有效的邮箱地址'
    return false
  }
  if (!password.value) {
    errorMessage.value = '请输入密码'
    return false
  }
  if (password.value.length < 6) {
    errorMessage.value = '密码长度至少为6位'
    return false
  }
  if (password.value !== confirmPassword.value) {
    errorMessage.value = '两次输入的密码不一致'
    return false
  }
  errorMessage.value = ''
  return true
}

// 注册处理函数
const handleRegister = async () => {
  if (!validateForm()) return
  
  isLoading.value = true
  
  try {
    // 使用认证服务进行注册
    await AuthService.register(username.value, email.value, password.value)
    
    // 注册成功后的处理
    successMessage.value = '注册成功！即将跳转到登录页面...'
    errorMessage.value = ''
    
    // 清除表单数据
    username.value = ''
    email.value = ''
    password.value = ''
    confirmPassword.value = ''
    
    // 3秒后跳转到登录页面
    setTimeout(() => {
      router.push('/shop-login')
    }, 3000)
  } catch (error) {
    errorMessage.value = error.message || '注册失败，请稍后重试'
    successMessage.value = ''
    console.error('注册错误:', error)
  } finally {
    isLoading.value = false
  }
}

// 返回登录页面
const goToLogin = () => {
  router.push('/shop-login')
}
</script>

<template>
  <div class="shop-register-container">
    <div class="shop-register-card">
      <div class="shop-register-header">
        <div class="shop-logo">
          <svg viewBox="0 0 24 24" width="48" height="48" stroke="#4caf50" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
            <circle cx="9" cy="21" r="1"></circle>
            <circle cx="20" cy="21" r="1"></circle>
            <path d="M1 1h4l2.68 13.39a2 2 0 0 0 2 1.61h9.72a2 2 0 0 0 2-1.61L23 6H6"></path>
          </svg>
          <h1>ShopApp</h1>
        </div>
        <h2>创建新账号</h2>
        <p>加入我们，享受购物乐趣</p>
      </div>
      
      <form class="shop-register-form" @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <div class="input-wrapper">
            <svg class="input-icon" viewBox="0 0 24 24" width="18" height="18" stroke="#999" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"></path>
              <circle cx="12" cy="7" r="4"></circle>
            </svg>
            <input
              type="text"
              id="username"
              v-model="username"
              placeholder="请输入用户名"
              class="form-input"
              :disabled="isLoading"
            />
          </div>
        </div>
        
        <div class="form-group">
          <label for="email">邮箱</label>
          <div class="input-wrapper">
            <svg class="input-icon" viewBox="0 0 24 24" width="18" height="18" stroke="#999" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z"></path>
              <polyline points="22,6 12,13 2,6"></polyline>
            </svg>
            <input
              type="email"
              id="email"
              v-model="email"
              placeholder="请输入邮箱地址"
              class="form-input"
              :disabled="isLoading"
            />
          </div>
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <div class="input-wrapper">
            <svg class="input-icon" viewBox="0 0 24 24" width="18" height="18" stroke="#999" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
              <path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
            </svg>
            <input
              type="password"
              id="password"
              v-model="password"
              placeholder="请输入密码（至少6位）"
              class="form-input"
              :disabled="isLoading"
            />
          </div>
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <div class="input-wrapper">
            <svg class="input-icon" viewBox="0 0 24 24" width="18" height="18" stroke="#999" stroke-width="2" fill="none" stroke-linecap="round" stroke-linejoin="round">
              <rect x="3" y="11" width="18" height="11" rx="2" ry="2"></rect>
              <path d="M7 11V7a5 5 0 0 1 10 0v4"></path>
            </svg>
            <input
              type="password"
              id="confirmPassword"
              v-model="confirmPassword"
              placeholder="请再次输入密码"
              class="form-input"
              :disabled="isLoading"
            />
          </div>
        </div>
        
        <div v-if="errorMessage" class="error-message">
          {{ errorMessage }}
        </div>
        
        <div v-if="successMessage" class="success-message">
          {{ successMessage }}
        </div>
        
        <button 
          type="submit" 
          class="register-button"
          :disabled="isLoading || successMessage"
        >
          {{ isLoading ? '注册中...' : '注册' }}
        </button>
        
        <div class="login-link">
          <span>已有账号？</span>
          <button type="button" class="link-button" @click="goToLogin">
            立即登录
          </button>
        </div>
      </form>
    </div>
  </div>
</template>

<style scoped>
.shop-register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
}

.shop-register-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 450px;
}

.shop-register-header {
  text-align: center;
  margin-bottom: 30px;
}

.shop-logo {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 20px;
}

.shop-logo h1 {
  margin: 15px 0 0 0;
  color: #4caf50;
  font-size: 28px;
  font-weight: 700;
}

.shop-register-header h2 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.shop-register-header p {
  margin: 0;
  color: #666;
  font-size: 16px;
}

.shop-register-form {
  display: flex;
  flex-direction: column;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  color: #333;
  font-weight: 500;
  font-size: 14px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.input-icon {
  position: absolute;
  left: 15px;
  pointer-events: none;
}

.form-input {
  width: 100%;
  padding: 12px 15px 12px 45px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #4caf50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}

.form-input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.error-message {
  background-color: #ffebee;
  color: #c62828;
  padding: 10px 15px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-size: 14px;
}

.success-message {
  background-color: #e8f5e8;
  color: #2e7d32;
  padding: 10px 15px;
  border-radius: 6px;
  margin-bottom: 20px;
  font-size: 14px;
}

.register-button {
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 14px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.register-button:hover:not(:disabled) {
  background-color: #45a049;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.3);
}

.register-button:active:not(:disabled) {
  transform: translateY(0);
}

.register-button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.login-link {
  margin-top: 20px;
  text-align: center;
  color: #666;
  font-size: 14px;
}

.link-button {
  background: none;
  border: none;
  color: #4caf50;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
  text-decoration: none;
  font-size: inherit;
}

.link-button:hover {
  text-decoration: underline;
}
</style>